package com.wang.study.service.impl;

import com.wang.study.commons.CommonsUtils;
import com.wang.study.mapper.UserMapper;
import com.wang.study.model.User;
import com.wang.study.service.UserService;
import com.wang.study.commons.MybatisUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserMapper userMapper;

    public UserServiceImpl() {
        userMapper = MybatisUtils.getMapper(UserMapper.class);
    }

    /**
     * 验证用户是否注册的业务
     * @param username
     * @return
     */
    @Override
    public Boolean checkUserName(String username) {
        User userDB = userMapper.findUserByUserName(username);
        if(null == userDB){
            return true;
        }else {
            return false;
        }
    }
    @Override
    public Boolean checkPassword(String password){
        if(null == password){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 验证邮箱是否被使用的业务
     * @param email
     * @return
     */
    @Override
    public Boolean checkEmail(String email) {
        User userDB = userMapper.findUserByEmail(email);
        if(null == userDB){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 业务：注册业务操作
     * @param user
     * @return
     */
    @Override
    public Map<String, String> register(User user) {
        HashMap<String, String> map = new HashMap<>();
        Integer i = userMapper.inserUser(user);
        if(i>0){
            //发邮件 用于用户激活
            CommonsUtils.getSendMail("yalishanda345@163.com",user.getEmail(),"JHCJMQJGQWWCNKDA",
            "邮件激活","点击链接<a href='http://127.0.0.1:8080/user.do?method=active&activeCode="+user.getActiveCode()+"'>"+user.getActiveCode()+"</a>");
            map.put("path","r:/commons/login.jsp");
        }else if(i==null){
            map.put("path","r:/commons/register.jsp");
        }else{
            map.put("path","r:/commons/register.jsp");
        }
        return map;
    }

    /**
     * 邮件激活业务操作
     * @param activeCode
     * @return
     */
    @Override
    public void active(String activeCode) {
        userMapper.updateUserStateByActiveCode(activeCode);
    }

    /**
     * 登录跳转业务操作
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> login(User user){
        //用于返回给业务层 跳转，响应
        HashMap<String, Object> map = new HashMap<>();
        //判断是否已经激活 默认没激活
        Boolean flag =false;
        User userDB = userMapper.selectByUser(user.getAccount());
        if(userDB.getAccount().equals(user.getAccount())&&userDB.getPassword().equals(user.getPassword())){
            //登陆成功
            //判断当前用户是否被激活
            if(userDB.getStatus()==1){//已经激活
                flag=true;
                if(userDB.getRole()==3){//普通用户
                    //跳转到用户首页
                    map.put("path","r:/user/index.jsp");
                }else{//管理员
                    //跳转到管理员首页
                    map.put("path","r:/admin/main.jsp");
                }
            }else{//没激活
                //返回登录页面
                map.put("message","当前用户尚未激活");
                map.put("path","r:/commons/login.jsp");
            }
        }else{
            //登录失败 密码错误
            map.put("message","密码错误");
            map.put("path","r:/commons/login.jsp");
        }
        //判断是否激活成功
        map.put("isSuccess",flag);
        //用户会话跟踪 判断是否登录
        map.put("user",userDB);
        return map;
    }

    @Override
    public List<User> findUserAll() {
        List<User> users= userMapper.findUserAll();
        return users;
    }

    @Override
    public Integer delUserById(Integer id) {
        Integer i= userMapper.delUserById(id);
        return i;
    }

    @Override
    public Integer addUser(String account,String password,String email,Integer status) {
        Integer i= userMapper.addUser(account,password,email,status);
        return i;
    }

    @Override
    public Integer updateUserById(String account,String password,String email,Integer status,Integer id) {
        Integer i= userMapper.updateUserById(account,password,email,status,id);
        return i;
    }

    @Override
    public Integer updatePwd(String username, String md5) {
        Integer i= userMapper.updatePwd(username,md5);
        return i;
    }

    @Override
    public User selectUserByUsername(String username) {

        return userMapper.selectUserByUsername(username);
    }

}
